package com.ruoyi.web.controller.epidemic;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.epidemic.domain.EpidemicUserDept;
import com.ruoyi.epidemic.domain.WxCpSysUser;
import com.ruoyi.epidemic.domain.WxCpSysUserBatch;
import com.ruoyi.epidemic.service.IEpidemicControlDeptService;
import com.ruoyi.epidemic.service.IEpidemicUserDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户管理【权限用户-审批】
 */
@Controller
@RequestMapping("/epidemic/poweruser")
public class PowerUserController extends BaseController {

    private String prefix = "epidemic/user/power";

    @Autowired
    private IEpidemicControlDeptService epidemicControlDeptService;

    @Autowired
    private IEpidemicUserDeptService epidemicUserDeptService;

    @RequiresPermissions("epidemic:poweruser:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @RequiresPermissions("epidemic:poweruser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxCpSysUser wxCpSysUser)
    {
        startPage();
        List<WxCpSysUser> list = epidemicControlDeptService.selectEpidemicUserList(wxCpSysUser);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap,String deptId) {
        mmap.put("writeDeptId", deptId);
        return prefix + "/add";
    }

    /**
     * 新增保存用户的关联关系
     */
    @RequiresPermissions("epidemic:poweruser:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxCpSysUserBatch wxCpSysUserBatch,String writeDeptId)
    {
        EpidemicUserDept epidemicUserDept = new EpidemicUserDept();
        epidemicUserDept.setDeptId(Long.parseLong(writeDeptId));
        epidemicUserDept.setPermissionType(wxCpSysUserBatch.getPermissionType());
        epidemicUserDept.setRemark(wxCpSysUserBatch.getRemark());
        int insertRow = 0;
        Long userIds[] = wxCpSysUserBatch.getUserIds();
        for (int i = 0; i < userIds.length; i++) {
            epidemicUserDept.setUserId(userIds[i]);
            //新增之前判断，若已存在则不再重复插入
            Integer m = epidemicUserDeptService.selectCountByUserIdAndDeptId(userIds[i],epidemicUserDept.getDeptId());
            if(m > 0){
                return error("当前用户对应的部门已存在，请勿重复添加！");
            }else{
                int j = epidemicUserDeptService.insertEpidemicUserDept(epidemicUserDept);
                if(j>0){
                    insertRow++;
                }else{
                    return error("数据插入失败！");
                }
            }
        }
        return toAjax(insertRow);
    }

    /**
     * 移除关联关系【并不删除用户基础信息】
     * @param ids
     * @return
     */
    @RequiresPermissions("epidemic:poweruser:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(epidemicUserDeptService.deleteEpidemicUserDeptByIds(ids));
    }

    /**
     * 修改用户关联关系
     */
    @RequiresPermissions("epidemic:poweruser:edit")
    @GetMapping("/edit/{userDeptId}")
    public String edit(@PathVariable("userDeptId") Long userDeptId, ModelMap mmap)
    {
        WxCpSysUser wxCpSysUser = epidemicControlDeptService.selectEpidemicUser(userDeptId);
        mmap.put("user", wxCpSysUser);
        return prefix + "/edit";
    }

    /**
     * 修改用户的关联关系
     */
    @RequiresPermissions("epidemic:poweruser:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated WxCpSysUser user)
    {
        EpidemicUserDept epidemicUserDept = new EpidemicUserDept();
        epidemicUserDept.setId(user.getUserDeptId());
        epidemicUserDept.setUserId(Long.parseLong(user.getUserId()));
        epidemicUserDept.setDeptId(user.getDeptId());
        epidemicUserDept.setPermissionType(user.getPermissionType());
        epidemicUserDept.setRemark(user.getRemark());
        return toAjax(epidemicUserDeptService.updateEpidemicUserDept(epidemicUserDept));
    }

    @GetMapping("/poweruserlist")
    public String poweruserlist(ModelMap mmap,String deptId)
    {
        mmap.put("deptId", deptId);
        return prefix + "/poweruserlist";
    }

    /**
     * 查询疫情防控信息收集列表
     */
    @PostMapping("/poweruserlistData")
    @ResponseBody
    public TableDataInfo poweruserlistData(EpidemicUserDept epidemicUserDept)
    {
        startPage();
        List<EpidemicUserDept> list = epidemicUserDeptService.selectEpidemicUserDeptList(epidemicUserDept);
        return getDataTable(list);
    }
}
